package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish (@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}",dishDTO);
        // 清理redis缓存
        clearRedisCache("dish_" + dishDTO.getCategoryId());
        dishService.addDish(dishDTO);
        return Result.success();
    }


    /**
     * 菜品分页条件查询
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> dishPageQuery (DishPageQueryDTO pageQueryDTO) {
        log.info("菜品分页查询:{}",pageQueryDTO);
        PageResult pageResult = dishService.dishPageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation(("根据id查询菜品"))
    @GetMapping("/{id}")
    public Result<DishVO> queryDishById (@PathVariable Long id) {
        DishVO dishVO = dishService.queryDishByIdWithFlavor(id);
        return Result.success(dishVO);
    }


    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteByIds (@RequestParam/*接收集合必须加这个注解*/ List<Long> ids) {
        log.info("批量删除菜品:{}",ids);

        clearRedisCache("dish_*");

        if (ids == null || ids.size() == 0) {
            throw new DeletionNotAllowedException(MessageConstant.EXIT_NULL_DATA);
        }
        dishService.deleteByIds(ids);
        return Result.success();
    }


    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish (@RequestBody DishDTO dishDTO) {
        log.info("更新菜品信息:{}",dishDTO);

        clearRedisCache("dish_*");

        dishService.updateDish(dishDTO);
        return Result.success();
    }


    @PostMapping("/status/{status}")
    @ApiOperation("菜品启售/停售")
    public Result changeStatus (@PathVariable Integer status,Long id) {
        log.info("菜品启售/停售:{},{}",status,id);
        clearRedisCache("dish_*");// 清除redis中的dish_*的数据
        dishService.changeStatus(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> qaueryDishByCategoryId (Long categoryId) {
         List<Dish> dishes = dishService.qaueryDishByCategoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 删除redis中所有符合pattern条件的key
     * @param pattern
     */
    private void clearRedisCache(String pattern) {
        // 从redis中获取所有以dish_开头的key
        Set keys = redisTemplate.keys(pattern);
        // 在redis中删除所有以dish_开头的key,这个delete方法支持传入一个collection集合
        redisTemplate.delete(keys);
    }
}
